package com.huluobo.a2109mouthdemo.contact;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.huluobo.a2109mouthdemo.R;
import com.huluobo.a2109mouthdemo.adapter.ContactAdapter;
import com.huluobo.a2109mouthdemo.base.BaseFragment;
import com.huluobo.a2109mouthdemo.chat.ChatActivity;

import java.util.ArrayList;
import java.util.List;

public class ContactFragment extends BaseFragment<ContactContract.View, ContactPresenter> implements ContactContract.View {

    private RecyclerView recyclerView;
    private List<ContactListItemBean> contactListItemBeans = new ArrayList<>();
    private ContactAdapter contactAdapter;

    @Override
    protected int bindLayoutId() {
        return R.layout.fragment_contact;
    }

    @Override
    protected void initData() {
        presenter = new ContactPresenter();
        presenter.loadContact();
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.contact_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
        contactAdapter = new ContactAdapter(contactListItemBeans);
        recyclerView.setAdapter(contactAdapter);

        contactAdapter.setOnItemClickListener((adapter, view, position) -> {
            String conversationId = contactListItemBeans.get(position).getUsername();
            Intent intent = new Intent(getActivity(), ChatActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("conversationId", conversationId);
            bundle.putInt("chatType", 1);
            intent.putExtras(bundle);
            startActivity(intent);
        });
    }

    @Override
    public void onLoadContactSuccess(List<ContactListItemBean> list) {
        getActivity().runOnUiThread(() -> {
            contactListItemBeans.clear();
            contactListItemBeans.addAll(list);
            contactAdapter.notifyDataSetChanged();
        });

    }

    @Override
    public void onLoadContactFailed(String error) {

    }
}